package com.financial.sys.service.bank.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.model.pojo.TBankCard;
import com.financial.model.pojo.TUserWallet;
import com.financial.sys.mapper.user.TUserWalletsMapper;
import com.financial.sys.mapper.bank.TBankCardsMapper;
import com.financial.sys.service.bank.TBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TBankCardServiceImpl extends ServiceImpl<TBankCardsMapper, TBankCard> implements TBankCardService {

    @Autowired
    private TUserWalletsMapper tUserWalletMapper;

    @Override
    public int addBankCard(TBankCard tBankCard) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tBankCard.setCreateTime(date);
        //获得用户id
        String accountId=tBankCard.getUserId();
        //查询该用户可用余额
        QueryWrapper<TUserWallet>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        TUserWallet tUserWallet=tUserWalletMapper.selectOne(queryWrapper);
        Long balance=tUserWallet.getAvailableAmount();
        //赋值给该用户银行卡余额
        tBankCard.setBalance(balance);
        return  baseMapper.insert(tBankCard);
    }

    @Override
    public TBankCard selectUserBankCard(String accountId) {
        QueryWrapper<TBankCard>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",accountId);
        TBankCard tBankCard=baseMapper.selectOne(queryWrapper);
        return tBankCard;
    }
}
